<template>
  <div class="page-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span class="title">结算方式管理</span>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>
            新增结算方式
          </el-button>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :inline="true" class="search-form">
        <el-form-item label="方式名称">
          <el-input v-model="searchForm.name" placeholder="请输入方式名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格 -->
      <el-table :data="tableData" border stripe>
        <el-table-column prop="id" label="编号" width="80" />
        <el-table-column prop="code" label="方式代码" width="120" />
        <el-table-column prop="name" label="方式名称" width="150" />
        <el-table-column prop="type" label="类型" width="120">
          <template #default="{ row }">
            <el-tag :type="getTypeTag(row.type)">{{ getTypeLabel(row.type) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="accountName" label="关联账户" width="150" />
        <el-table-column prop="feeRate" label="手续费率" width="100">
          <template #default="{ row }">
            {{ row.feeRate ? row.feeRate + '%' : '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="isDefault" label="默认方式" width="100">
          <template #default="{ row }">
            <el-tag :type="row.isDefault ? 'success' : 'info'">
              {{ row.isDefault ? '是' : '否' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'danger'">
              {{ row.status === 1 ? '启用' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" min-width="150" />
        <el-table-column label="操作" width="180" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button link type="danger" @click="handleDelete(row)">删除</el-button>
            <el-button 
              link 
              :type="row.status === 1 ? 'warning' : 'success'" 
              @click="handleToggleStatus(row)"
            >
              {{ row.status === 1 ? '停用' : '启用' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.size"
        :total="pagination.total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSearch"
        @current-change="handleSearch"
      />
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog 
      v-model="dialogVisible" 
      :title="dialogTitle"
      width="600px"
    >
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="方式代码" prop="code">
          <el-input v-model="form.code" placeholder="请输入方式代码" />
        </el-form-item>
        <el-form-item label="方式名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入方式名称" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option label="现金" value="cash" />
            <el-option label="银行卡" value="bank_card" />
            <el-option label="支付宝" value="alipay" />
            <el-option label="微信" value="wechat" />
            <el-option label="转账" value="transfer" />
            <el-option label="支票" value="check" />
            <el-option label="其他" value="other" />
          </el-select>
        </el-form-item>
        <el-form-item label="关联账户" prop="accountId">
          <el-select v-model="form.accountId" placeholder="请选择关联账户" clearable>
            <el-option label="现金账户" :value="1" />
            <el-option label="银行账户" :value="2" />
            <el-option label="支付宝账户" :value="3" />
            <el-option label="微信账户" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="手续费率" prop="feeRate">
          <el-input-number v-model="form.feeRate" :min="0" :max="100" :precision="2" :step="0.1" />
          <span class="form-tip">单位：%</span>
        </el-form-item>
        <el-form-item label="默认方式" prop="isDefault">
          <el-switch v-model="form.isDefault" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="0" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input 
            v-model="form.remark" 
            type="textarea" 
            :rows="3"
            placeholder="请输入备注信息" 
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

// 搜索表单
const searchForm = reactive({
  name: ''
})

// 表格数据
const tableData = ref([
  { id: 1, code: 'CASH', name: '现金', type: 'cash', accountName: '现金账户', accountId: 1, feeRate: 0, isDefault: true, sort: 1, status: 1, remark: '现金支付' },
  { id: 2, code: 'BANK', name: '银行卡', type: 'bank_card', accountName: '银行账户', accountId: 2, feeRate: 0.6, isDefault: false, sort: 2, status: 1, remark: '银行卡支付' },
  { id: 3, code: 'ALIPAY', name: '支付宝', type: 'alipay', accountName: '支付宝账户', accountId: 3, feeRate: 0.6, isDefault: false, sort: 3, status: 1, remark: '支付宝支付' },
  { id: 4, code: 'WECHAT', name: '微信支付', type: 'wechat', accountName: '微信账户', accountId: 4, feeRate: 0.6, isDefault: false, sort: 4, status: 1, remark: '微信支付' },
  { id: 5, code: 'TRANSFER', name: '银行转账', type: 'transfer', accountName: '银行账户', accountId: 2, feeRate: 0, isDefault: false, sort: 5, status: 1, remark: '银行转账' },
  { id: 6, code: 'CHECK', name: '支票', type: 'check', accountName: '银行账户', accountId: 2, feeRate: 0, isDefault: false, sort: 6, status: 1, remark: '支票支付' }
])

// 分页
const pagination = reactive({
  page: 1,
  size: 10,
  total: 6
})

// 对话框
const dialogVisible = ref(false)
const dialogTitle = ref('新增结算方式')
const formRef = ref(null)

// 表单数据
const form = reactive({
  id: null,
  code: '',
  name: '',
  type: '',
  accountId: null,
  feeRate: 0,
  isDefault: false,
  sort: 0,
  status: 1,
  remark: ''
})

// 表单验证规则
const rules = {
  code: [{ required: true, message: '请输入方式代码', trigger: 'blur' }],
  name: [{ required: true, message: '请输入方式名称', trigger: 'blur' }],
  type: [{ required: true, message: '请选择类型', trigger: 'change' }]
}

// 获取类型标签
const getTypeTag = (type) => {
  const tagMap = {
    cash: 'success',
    bank_card: 'primary',
    alipay: 'primary',
    wechat: 'success',
    transfer: 'warning',
    check: 'info',
    other: ''
  }
  return tagMap[type] || ''
}

// 获取类型名称
const getTypeLabel = (type) => {
  const labelMap = {
    cash: '现金',
    bank_card: '银行卡',
    alipay: '支付宝',
    wechat: '微信',
    transfer: '转账',
    check: '支票',
    other: '其他'
  }
  return labelMap[type] || type
}

// 搜索
const handleSearch = () => {
  console.log('搜索:', searchForm)
  ElMessage.success('查询成功')
}

// 重置
const handleReset = () => {
  searchForm.name = ''
  handleSearch()
}

// 新增
const handleAdd = () => {
  dialogTitle.value = '新增结算方式'
  Object.assign(form, {
    id: null,
    code: '',
    name: '',
    type: '',
    accountId: null,
    feeRate: 0,
    isDefault: false,
    sort: 0,
    status: 1,
    remark: ''
  })
  dialogVisible.value = true
}

// 编辑
const handleEdit = (row) => {
  dialogTitle.value = '编辑结算方式'
  Object.assign(form, { ...row })
  dialogVisible.value = true
}

// 删除
const handleDelete = (row) => {
  ElMessageBox.confirm(`确定要删除结算方式 "${row.name}" 吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    console.log('删除:', row.id)
    ElMessage.success('删除成功')
  })
}

// 切换状态
const handleToggleStatus = (row) => {
  const status = row.status === 1 ? 0 : 1
  const text = status === 1 ? '启用' : '停用'
  ElMessageBox.confirm(`确定要${text}结算方式 "${row.name}" 吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    row.status = status
    ElMessage.success(`${text}成功`)
  })
}

// 提交表单
const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      console.log('提交:', form)
      ElMessage.success(form.id ? '修改成功' : '新增成功')
      dialogVisible.value = false
      handleSearch()
    }
  })
}
</script>

<style scoped>
.page-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.search-form {
  margin-bottom: 20px;
}

.el-pagination {
  margin-top: 20px;
  justify-content: flex-end;
}

.form-tip {
  margin-left: 10px;
  color: #999;
  font-size: 12px;
}
</style>

